package com.spider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.utils.HttpClient;
import com.spider.utils.KuaiProxy;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test1 {
    public static void main(String[] args) {
        try {
            Map<String, String> proxy = KuaiProxy.getProxy();
            String ip = null;
            String port = null;
            for (String key : proxy.keySet()) {
                ip = key;
                port = proxy.get(key);
            }
            String query = URLEncoder.encode("李云龙", "UTF-8");
            JSONObject obj = HttpClient.get("https://pic.sogou.com/napi/pc/searchList?mode=1&start=48&xml_len=48&query=" + query, "utf-8", ip, Integer.valueOf(port));
            ExecutorService executor = Executors.newFixedThreadPool(10);
            JSONObject data = (JSONObject) obj.get("data");
            JSONArray array = (JSONArray) data.get("items");
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    int count = 0;
                    for (Object arr : array) {
                        JSONObject res = JSONObject.parseObject(arr.toString());
                        System.out.println(res.getString("oriPicUrl"));
                        HttpClient.downloadFile(res.getString("oriPicUrl"), "1", count + "." + res.getString("oriPicUrl").substring(res.getString("oriPicUrl").lastIndexOf(".") + 1));
                        count++;
                    }
                }
            });
            executor.shutdown();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
